/*
 * This source code is part of TWaver 4.0
 *
 * Serva Software PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 * Copyright 2002 - 2011 Serva Software. All rights reserved.
 */

package demo.table.person;

import twaver.AlarmSeverity;
import twaver.TDataBox;
import twaver.TWaverUtil;
import twaver.table.TElementTable;

public class PersonDataFactory {
	
	public static void initBox(TElementTable table, TDataBox box, int startIndex){
//		long start = System.currentTimeMillis();
		table.setLazyPublishMode(true);
//		box.startBatch();
//		table.getTableModel().lock();
		
		for(int i=0; i<1; i++){
			box.addElement(createPerson(startIndex++, "Celso", "Aguiar"));
			box.addElement(createPerson(startIndex++, "Puneet", "Ahuja"));
			box.addElement(createPerson(startIndex++, "Adam", "Altman"));
			box.addElement(createPerson(startIndex++, "Steve", "Amerige"));
			box.addElement(createPerson(startIndex++, "Art", "Amezcua"));
			box.addElement(createPerson(startIndex++, "Chaitanya", "Atreya"));
			box.addElement(createPerson(startIndex++, "Dylan", "Ashe"));
			box.addElement(createPerson(startIndex++, "Amit", "Batra"));
			box.addElement(createPerson(startIndex++, "Gary", "Beardsley"));
			box.addElement(createPerson(startIndex++, "Gangadhar", "Bathula"));
			box.addElement(createPerson(startIndex++, "Akiko", "Birk"));
			box.addElement(createPerson(startIndex++, "Treve", "Bonser"));
			box.addElement(createPerson(startIndex++, "Alexander", "Born"));
			box.addElement(createPerson(startIndex++, "Jason", "Boyer"));
			box.addElement(createPerson(startIndex++, "Foster", "Brereton"));
			box.addElement(createPerson(startIndex++, "Christopher", "Brichford"));
			box.addElement(createPerson(startIndex++, "John", "Briere"));
		}
		
//		table.getTableModel().unlockAndPublish();
//		box.endBatch();
//		System.out.println(box.size() + "|" + (System.currentTimeMillis()-start));
	}

	public static Person createPerson(int id, String firstName, String lastName){
		// create element object
		Person person = new Person("(" + id + ")");
		
		// set element's javabean properties
		person.setFirstName(firstName);
		person.setLastName(lastName);
		person.setAge(TWaverUtil.getRandomInt(100));
		
		// set element's client properties
		person.putClientProperty("birth", 2007-person.getAge());

		// set element's user properties
		person.putUserProperty("icon", "/demo/resource/images/attachment" + (TWaverUtil.getRandomInt(6) + 1) + ".png");
		
		// create business object
		Profile bo = new Profile();
		
		// set bo's javabean properties
		bo.setSalary((TWaverUtil.getRandomInt(10) + 2) * 1000);
		bo.setMarried(TWaverUtil.getRandomBool());
		
		// set bo's nested data
		NestedData nestedData = new NestedData();
		nestedData.setSex(TWaverUtil.getRandomBool());
		nestedData.getElement().getAlarmState().setPropagateSeverity(AlarmSeverity.getRandomSeverity());
		bo.setNested(nestedData);
		bo.getNested().getElement().getAlarmState().getPropagateSeverity();

		// set bo's client properties
		bo.putClientProperty("description", person.getFullName() + " with monthly salary of $" + bo.getSalary());
	
		person.setBusinessObject(bo);
		
		return person;
	}
}